package com.zyx.flinkdemo.stream.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static com.zyx.flinkdemo.stream.cons.CommonConfig.HABSE_SCHEMA;
import static com.zyx.flinkdemo.stream.cons.CommonConfig.PHOENIX_URL;

/**
 * @author zyx
 * @since 2021/6/10 22:08
 * desc: 从Phoenix中查询数据
 */
public class PhoenixUtil {
    private static Connection conn = null;

    public static void init() {
        try {
            //注册驱动
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
            //获取Phoenix的连接
            Properties prop = new Properties();
            prop.setProperty("phoenix.schema.isNamespaceMappingEnabled", String.valueOf(true));
            prop.setProperty("phoenix.schema.mapSystemTablesToNamespace", String.valueOf(true));
            conn = DriverManager.getConnection(PHOENIX_URL, prop);
            //指定操作的表空间
            conn.setSchema(HABSE_SCHEMA);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从Phoenix中查询数据
     * @param sql 查询的sql语句
     * @param clazz 返回类型
     * @param <T> 返回值类型的泛型
     * @return 返回值
     */
    public static <T> List<T> queryList(String sql, Class<T> clazz) {
        if (conn == null) {
            init();
        }
        List<T> resultList = new ArrayList<>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //执行SQL语句
            rs = ps.executeQuery();
            //通过结果集对象获取元数据信息
            ResultSetMetaData metaData = rs.getMetaData();
            //处理结果集
            while (rs.next()) {
                //声明一个对象，用于封装查询的一条结果集
                T rowData = clazz.newInstance();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    BeanUtils.setProperty(rowData, metaData.getColumnName(i), rs.getObject(i));
                }
                resultList.add(rowData);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("从维度表中查询数据失败");
        } finally {
            //释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 200; i++) {
            System.out.println("第" + i + "次开始时间:" + Instant.now());
            System.out.println(queryList("select * from \"user\"", JSONObject.class));
            System.out.println("第" + i + "次结束时间:"+ Instant.now());
        }
    }
}
